package com.biz.home.controller;

import com.biz.home.feign.*;
import com.netflix.hystrix.contrib.javanica.annotation.*;
import lombok.extern.slf4j.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.*;

import javax.annotation.*;

/**
 * @Description
 * @Author liuxi
 * @Date 2024/5/18
 **/
@RestController
@Slf4j
public class HomeController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private SkuService skuService;

    private static final String GET_SKU_INFO_URL = "http://APE-CLOUD-SKU/test";

    @GetMapping("/test")
    public String test() {
        return "Hello World";
    }

    @GetMapping("/getSkuInfo")
    public String getSkuInfo() {
        return restTemplate.getForObject(GET_SKU_INFO_URL, String.class);
    }

    @GetMapping("/getSkuInfoV2")
    public String getSkuInfoV2() {
        return skuService.test();
    }

    @GetMapping("/testTimeout")
    public String testTimeout() {
        return skuService.testTimeout();
    }

    @GetMapping("/testHystrix")
    public String testHystrix() {
        return skuService.testHystrix();
    }

    /**
     * 调用方服务降级
     */
    @GetMapping("/testHystrixTimeout")
    @HystrixCommand(fallbackMethod = "testHystrixHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
    public String testHystrixTimeout() {
        return skuService.testHystrixTimeout();
    }

    public String testHystrixHandler() {
        return "调用方主动接口降级";
    }

}
